package cz.acies.data.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cz.acies.data.persist.ModelObject;
import cz.acies.data.persist.UserPerson;
import cz.acies.uniql.sql.InsertQuery;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.UpdateQuery;
import cz.acies.uniql.sql.element.UniQLCondition;
import cz.acies.uniql.sql.element.UniQLJoin;
import cz.acies.utils.UIDGenerator;

public class UserPersonDAO extends DefaultDAO
{

	public UserPersonDAO() {
		this.tableName = "user_person";
	}
	
	
	public List<Map<String, Object>> selectAll() {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		try {
			//QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd())
			.addField("user_person.*")
			.addField("mail_address.mailAddress AS email")
			.addField("user_login.userName AS userName")
			.addFrom(""+tableName)
			.add(new UniQLJoin()
				.addFrom("mail_address")
				.add(new UniQLCondition("mail_address.id", "=", "user_person.mailId"))
			).add(new UniQLJoin()
				.addFrom("user_login")
				.add(new UniQLCondition("user_person.id", "=", "user_login.personId"))
			);;
			data = qr().query(sql.toSQLString(), new DefaultMapListHandler(new DefaultRowProcessor()));
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}
	
	@Override
	public List<ModelObject> selectAll(String tableName, Class c) {
		List<ModelObject> data = new ArrayList<ModelObject>();
		try {
			//this.tableName = "user_person";
			//QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd())
			.addField("user_person.*")
			.addField("mail_address.mailAddress AS email")
			.addFrom(""+this.tableName)
			.add(new UniQLJoin()
				.addFrom("mail_address")
				.add(new UniQLCondition("mail_address.id", "=", "user_person.mailId"))
			);  
			ResultSetHandler<List<ModelObject>> handler = new BeanListHandler<ModelObject>(c);
			data = qr().query(sql.toSQLString(), handler);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}

	public UserPerson loadDeatail(String id) {
		UserPerson person = new UserPerson();
		try {
			//QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd())
			.addFrom(""+tableName)
			.add(new UniQLCondition(tableName+".id", "=", "'" + id +"'"));
			
			ResultSetHandler<List<UserPerson>> handler = new BeanListHandler<UserPerson>(UserPerson.class);
			List<UserPerson> data = qr().query(sql.toSQLString(), handler);
			if (!data.isEmpty()) {
				person = data.get(0);
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return person;
	}
	
	public UserPerson doAppend(UserPerson person) {
		try {
			person.setId(UIDGenerator.createUID("WX-R"));
			InsertQuery sql = new InsertQuery(dbmd())
				.addFrom("user_person")
				.addParam("id",""+person.getId())
				.addParam("firstName", ""+((person.hasFirstName()) ? person.getFirstName() : "''"))
				.addParam("lastName", ""+((person.hasLastName()) ? person.getLastName() : "''"))
				//.addParam("email", ""+((person.hasEmail()) ? person.getEmail() : "''"))
				;
			qr().update(sql.toSQLString(true));
			
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return person;
	}
	
	public UserPerson doUpdate(UserPerson person) {
		try {
			if (!person.hasId()) {
				return this.doAppend(person);
			}
			UpdateQuery sql = new UpdateQuery(dbmd())
				.addFrom("user_person")
				.addParam("firstName", ""+((person.hasFirstName()) ? person.getFirstName() : "''"))
				.addParam("lastName", ""+((person.hasLastName()) ? person.getLastName() : "''"))
				//.addParam("email", ""+((person.hasEmail()) ? person.getEmail() : "''"))
				.add(new UniQLCondition("id","=","'"+person.getId()+"'"));
			qr().update(sql.toSQLString(true));
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return person;
	}
}
